﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace fbasync.web.Controls
{
    public interface IExceptionHandler
    {
        void HandleException(Exception ex);
    }

    public static class ExceptionHandler
    {
        public delegate void HandleExceptionEvent(Exception ex);

        private static IList<IExceptionHandler> RegisteredHandlers
        {
            get
            {
                IList<IExceptionHandler> result = (IList<IExceptionHandler>)HttpContext.Current.Items["ExceptionHandlerRegisteredHandlers"];

                if (null == result)
                {
                    result = new List<IExceptionHandler>();

                    HttpContext.Current.Items["ExceptionHandlerRegisteredHandlers"] = result;
                }

                return result;
            }
        }

        public static void HandleException(Exception ex)
        {
            foreach (IExceptionHandler handler in RegisteredHandlers)
            {
                handler.HandleException(ex);
            }
        }

        public static void RegisterExceptionHandler(IExceptionHandler handler)
        {
            if (RegisteredHandlers.IndexOf(handler) == -1)
            {
                RegisteredHandlers.Add(handler);
            }
        }
    }

    public static class Consts
    {
        public const long UnknownBundleId = long.MaxValue;
        public const int UnknownUniqueBundleId = -1;
        public const int UnknownInternalId = -1;

        public const string NewBundleName = "__Bundle{0}"; // The parameter is uniqueBundleId; 
        public const string NewOnelineTemplateTitle = "{{*actor*}} does something in {0}"; // The parameter is application name
        public const string FakeFullStoryTitle = "{*actor*} is adding a new template";
        public const string CustomApplicationAppKey = "-1";
        public const string DefaultConfigFileName = "facebookasync";
    }

    public static class Utils
    {
        public static int GetStoryHashCode(StoryTemplateBundle story)
        {
            StringBuilder builder = new StringBuilder();

            GetTemplateString(builder, story.FullStory);

            if (null != story.OneLineStories)
            {
                foreach (StoryTemplate onelineTemplate in story.OneLineStories)
                {
                    GetTemplateString(builder, onelineTemplate);
                }
            }

            if (null != story.ShortStories)
            {
                foreach (StoryTemplate shortStoryTemplate in story.ShortStories)
                {
                    GetTemplateString(builder, shortStoryTemplate);
                }
            }

            return builder.ToString().GetHashCode();
        }

        private static void GetTemplateString(StringBuilder builder, StoryTemplate storyTemplate)
        {
            if (storyTemplate != null)
            {
                builder.Append(storyTemplate.template_title);
                builder.Append(storyTemplate.template_body);
            }
        }
    }
}
